package cn.anc.aonicardv.module.ui.my;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import cn.anc.aonicardv.Configs;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.ActivityUtils;
import cn.anc.aonicardv.util.DataClearManager;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.util.LanguageConstants;
import cn.anc.aonicardv.util.LogUtil;
import cn.anc.aonicardv.util.NetUtils;
import cn.anc.aonicardv.util.SpUtils;
import cn.anc.aonicardv.util.ui.UIUtil;
import cn.anc.aonicardv.widget.CommonItemLayout;
import cn.anc.aonicardv.widget.CustomDialog;
import cn.anc.httpcontrolutil.cache.CacheManager;

/**
 * Created by yangdai on 2017/6/22.
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mIvSwichConnect;
    private View mTvLogLine;
    private LinearLayout mLayoutConnect;
    private CommonItemLayout mCilSettingAbout, mCilSettingLog, mCilSettingClear, mCilSettingHelp, mCilSettingLanguage, mCilSettingConnectList;
    private boolean isSlecetAuto;
    private DialogUtils dialogUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initVariable() {
        initView();
    }

    @Override
    public void setListener() {
    }

    @Override
    public void initData() {
    }

    private void initView() {
        int mPx = UIUtil.dip2px(this, 1);

        findViewById(R.id.tv_back).setOnClickListener(this);
        dialogUtils = new DialogUtils();
        TextView mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setOnClickListener(this);
        mTvTitle.setText(getString(R.string.my_setting));

        mLayoutConnect = findViewById(R.id.layout_view_recorder_settings_connect);
        mLayoutConnect.setVisibility(View.GONE);
        mIvSwichConnect = (ImageView) findViewById(R.id.iv_recorder_settings_connect);
        mIvSwichConnect.setOnClickListener(this);

        //语言
        mCilSettingLanguage = (CommonItemLayout) findViewById(R.id.cil_settings_language);
        if (Configs.DEFUTL_SELECT_LANGUAGE) {
            mCilSettingLanguage.setVisibility(View.VISIBLE);
            mCilSettingLanguage.setOnClickListener(this);
        }
        //帮助
        mCilSettingHelp = (CommonItemLayout) findViewById(R.id.cil_settings_help);
        mCilSettingHelp.setOnClickListener(this);
        //清除缓存
        mCilSettingClear = (CommonItemLayout) findViewById(R.id.cil_settings_clear);
        mCilSettingClear.setOnClickListener(this);
        //链接记录
        mCilSettingConnectList = (CommonItemLayout) findViewById(R.id.cil_settings_connect_list);
        if (Configs.DEFUTL_SHOW_CONNECT_LIST==1) {
            mCilSettingLanguage.setVisibility(View.VISIBLE);
            mCilSettingLanguage.setOnClickListener(this);
        }

        //关于
        mCilSettingAbout = (CommonItemLayout) findViewById(R.id.cil_settings_about);
        mCilSettingAbout.setOnClickListener(this);


         mTvLogLine =  findViewById(R.id.tv_log);
        mCilSettingLog = (CommonItemLayout) findViewById(R.id.cil_settings_loginfo);
        mCilSettingLog.setOnClickListener(this);

    }


    @Override
    protected void onResume() {
        super.onResume();
        setLanguageRightText();
        setCacheRightText();
        isSlecetAuto = SpUtils.getBoolean(Constant.SpKeyParam.AUTO_CONNECT_RECORDER, true);
        setMobileNetwork();
    }

    private boolean isOpenMobileNetwork = false;

    private void setMobileNetwork() {
        isOpenMobileNetwork = NetUtils.getMobileDataStatus(this);
        LogUtil.e("llcTest1019","-------getMobileDataStatus:"+isOpenMobileNetwork);
        if (isOpenMobileNetwork) {
            mIvSwichConnect.setImageDrawable(getResources().getDrawable(R.mipmap.switch_btn_open));
        } else {
            mIvSwichConnect.setImageDrawable(getResources().getDrawable(R.mipmap.switch_btn_close));
        }
    }

    private void switchMobileNetwork() {
        boolean enabled =  NetUtils.setMobileDataEnabled(this,!isOpenMobileNetwork);
        LogUtil.e("llcTest1019","-------setMobileDataEnabled:"+enabled);
        if (enabled){
            isOpenMobileNetwork = !isOpenMobileNetwork;
            mIvSwichConnect.setImageDrawable(getResources().getDrawable(isOpenMobileNetwork?R.mipmap.switch_btn_open:R.mipmap.switch_btn_close));
        }
    }


    /**
     * 设置语言显示
     */
    private void setLanguageRightText() {
        String selectedLanguage = SpUtils.getString(Constant.SpKeyParam.APP_LANUAGE, Configs.DEFUTL_LANGUAGE);
        mCilSettingLanguage.setRightTextVisibility(View.VISIBLE);
        if (selectedLanguage.equals(LanguageConstants.SIMPLIFIED_CHINESE)) {
            mCilSettingLanguage.setRightText(LanguageConstants.SIMPLIFIED_CHINESE_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.ENGLISH)) {
            mCilSettingLanguage.setRightText(LanguageConstants.ENGLISH_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.TRADITIONAL_CHINESE)) {
            mCilSettingLanguage.setRightText(LanguageConstants.TRADITIONAL_CHINESE_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.LITHUANIAN)) {
            mCilSettingLanguage.setRightText(LanguageConstants.LITHUANIAN_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.ESTONIAN)) {
            mCilSettingLanguage.setRightText(LanguageConstants.ESTONIAN_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.LATVIAN)) {
            mCilSettingLanguage.setRightText(LanguageConstants.LATVIAN_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.POLISH)) {
            mCilSettingLanguage.setRightText(LanguageConstants.POLISH_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.ROMANIA)) {
            mCilSettingLanguage.setRightText(LanguageConstants.ROMANIA_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.RUSSIAN)) {
            mCilSettingLanguage.setRightText(LanguageConstants.RUSSIAN_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.UKRAINE)) {
            mCilSettingLanguage.setRightText(LanguageConstants.UKRAINE_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.SLOVAK)) {
            mCilSettingLanguage.setRightText(LanguageConstants.SLOVAK_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.SPANISH)) {
            mCilSettingLanguage.setRightText(LanguageConstants.SPANISH_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.GERMAN)) {
            mCilSettingLanguage.setRightText(LanguageConstants.GERMAN_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.ITALIAN)) {
            mCilSettingLanguage.setRightText(LanguageConstants.ITALIAN_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.FRENCH)) {
            mCilSettingLanguage.setRightText(LanguageConstants.FRENCH_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.CZECH)) {
            mCilSettingLanguage.setRightText(LanguageConstants.CZECH_VALUE);
        } else if (selectedLanguage.equals(LanguageConstants.HUNGARY)) {
            mCilSettingLanguage.setRightText(LanguageConstants.HUNGARY_VALUE);
        }
    }


    /**
     * 设置缓存显示
     */
    private void setCacheRightText() {
        mCilSettingClear.setRightTextVisibility(View.VISIBLE);
        String cacheStr = "";
        File file = new File(CacheManager.CACHE_ROOT_DIR);
        if (file.exists())
            try {
                cacheStr = DataClearManager.getCacheSize(file.getAbsoluteFile());
            } catch (Exception e) {
                e.printStackTrace();
            }
        mCilSettingClear.setRightText(TextUtils.isEmpty(cacheStr) ? "0.00MB" : cacheStr);
    }

    // 需要点击几次 就设置几
    long[] mHits = null;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_back:
                finish();
                break;


            //语言设置
            case R.id.cil_settings_language:
                ActivityUtils.jumpActivity(this, SwitchAppLanuageActivity.class);
                break;

            //清除缓存
            case R.id.cil_settings_clear:
                clearCache();
                break;
            //切换自动连接
            case R.id.iv_recorder_settings_connect:
                switchMobileNetwork();
                break;
            //帮助
            case R.id.cil_settings_help:
                ActivityUtils.jumpActivity(this, HelpActivity.class);
                break;
            //链接记录
            case R.id.cil_settings_connect_list:
                break;

            //关于我们
            case R.id.cil_settings_about:
                ActivityUtils.jumpActivity(this, AboutActivity.class);
                break;

            case R.id.cil_settings_loginfo:
                ActivityUtils.jumpActivity(this, LogActivity.class);
                break;


            case R.id.tv_title:
                if (mHits == null) {
                    mHits = new long[5];
                }
                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);//把从第二位至最后一位之间的数字复制到第一位至倒数第一位
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();//记录一个时间
                if (SystemClock.uptimeMillis() - mHits[0] <= 2000) {//两秒内连续点击。
                    mHits = null;    //这里说明一下，我们在进来以后需要还原状态，否则如果点击过快，第十一次，第十二次 都会不断进来触发该效果。重新开始计数即可
                    mTvLogLine.setVisibility(View.VISIBLE);
                    mCilSettingLog.setVisibility(View.VISIBLE);
                }


                break;

            default:
                break;
        }
    }


    private CustomDialog mClearialog;

    public void clearCache() {
        if (mCilSettingClear.getRightText().equals("0.00MB")) {
            return;
        }
        if (mClearialog == null) {
            mClearialog = new CustomDialog.AlertBuilder(this)
                    .setContentText(getString(R.string.my_clear_cache_alert))
                    .setNegativeBtn(getString(R.string.ok), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            ProgressDialog progressDialog = dialogUtils.getProgressDialog(SettingActivity.this);
                            progressDialog.setMessage(getString(R.string.my_clearing_cache));
                            progressDialog.setCancelable(false);
                            progressDialog.show();
                            File file = new File(CacheManager.CACHE_ROOT_DIR);
                            if (file.exists())
                                DataClearManager.deleteFolderFile(file.getAbsolutePath(), true);
                            else
                                return;
                            Toast.makeText(SettingActivity.this, getString(R.string.my_clear_cache_hint), Toast.LENGTH_SHORT).show();
                            mCilSettingClear.setRightText("0.00MB");
                            progressDialog.dismiss();
                        }
                    })
                    .setPositiveBtn(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).createPromptDialog();
            mClearialog.setCancelable(true);
            mClearialog.setCanceledOnTouchOutside(true);
        }
        mClearialog.show();


    }


}

